package com.multicampus.sample.lucene;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.cjk.CJKAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Searcher;

public class FileSystemSearcher {
	public static void main(String[] args) {
		if (args.length > 0 && ("-h".equals(args[0]) || "-help".equals(args[0]))) {
			System.out.println("Usage: java org.apache.lucene.demo.SearchFiles searchWord");
			System.exit(0);
		}

		String field = "contents";

		try {
			IndexReader reader = IndexReader.open("/data/index/filesystem");
			Searcher searcher = new IndexSearcher(reader);

			Analyzer analyzer = new CJKAnalyzer();

			QueryParser parser = new QueryParser(field, analyzer);
			Query query = parser.parse(args[0]);
			System.out.println("Searching for: " + query.toString(field));

			Hits hits = searcher.search(query);

			for (int i = 0; i < hits.length(); i++) {
				Document doc = hits.doc(i);
				System.out.println("Path: " + doc.get("path"));
				System.out.println("   Title: " + doc.get("title"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
